package cate.game.play.skill.passive.talent;

import cate.common.util.XT;
import cate.game.attr.FightAttr;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.AppendMove;
import cate.game.play.proce.move.AppendMoveHandler;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.proce.result.SkillAnounceResult;
import cate.game.play.proce.turn.TurnCtx;
import cate.game.play.skill.Skill;
import cate.game.play.skill.passive.PassiveHandler;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

@NoteClass("连击")
public class 连击PH extends PassiveHandler implements AppendMoveHandler {

	@NoteField("物连")
	private boolean supportPhy;

	@NoteField("法连")
	private boolean supportMagic;

	@NoteField("概率")
	private int chance;

	@NoteField("血量调整")
	private double hpChangeRate;

	@NoteField("临时属性")
	private FightAttr moveAttr;

	//物连=1&法连=0&概率=10000&血量调整=-5000&临时属性=60:-5000
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		this.supportPhy = args.getBoolean("物连", false);
		this.supportMagic = args.getBoolean("法连", false);
		this.chance = args.getInt("概率", 10000);
		this.hpChangeRate = args.getWDouble("血量调整", -5000d);
		this.moveAttr = new FightAttr(args.get("临时属性"));
	}


	@Override
	public void onOwnerMoveDone(MoveCtx move,TimeActionCtx action) {
		if (move.isAppend()) {
			return;
		}
		if (skill.owner.isDead()) {
			return;
		}
		Skill moveSkill = move.getSkill();
		if (moveSkill.cfg.getFeaturePhy() && this.supportPhy || (moveSkill.cfg.getFeatureMagic() && this.supportMagic)) {
			if (!XT.chance(this.getChance())) {
				return;
			}
			AppendMove append = new AppendMove(moveSkill.owner, moveSkill, this);
			append.hpChangeRate = hpChangeRate;
			append.moveAttr = moveAttr.copy();
			move.getTurn().addAppend(append);
		}
	}

	private int getChance() {
		int chance = this.chance;
		chance += (int) this.skill.owner.attr.total().getDoubleActChance(this.supportPhy);
		return Math.max(chance, 0);
	}

	@Override
	public boolean inValid(TurnCtx turn, AppendMove appendMove) {
		return false;
	}

	@Override
	public void beforeAppendMove(TurnCtx turn, AppendMove appendMove) {
		//通知全场的人有人要连击了
		TimeActionCtx action = turn.getRound().buildTimeAction();
		turn.getRound().getPlay().teamVS.forAreaFighters(a -> a.skill.forHandlers(h -> h.onDoubleAct(turn,action, skill.owner,skill.tid)));
		turn.buildTimeAction().addResult(new SkillAnounceResult().skillSet(this.skill));
	}

	@Override
	public void afterAppendMove(TurnCtx turn, AppendMove appendMove) {

	}

	@Override
	public void onAppendMoveFail(TurnCtx turn, AppendMove appendMove) {

	}
}
